package com.yuuzy.view;

import com.yuuzy.dao.Buy_WipDAO;
import com.yuuzy.dao.ClientDAO;
import com.yuuzy.dao.WipDAO;
import com.yuuzy.model.Buy_Wip;
import com.yuuzy.model.Client;
import com.yuuzy.model.Wip;
import com.yuuzy.utils.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

public class BuyWip extends JInternalFrame {


    private WipDAO wipDAO = new WipDAO();
    private Buy_WipDAO buy_wipDAO = new Buy_WipDAO();


    private JTextField BI_nameText;
    private JTable wip_table;
    private JTextField wip_idTxt;
    private JTextField wip_nameTxt;
    private JTextField wip_desTxt;
    private JTextField wip_amountTxt;
    private JTextField wip_yearTxt;
    private JTextField client_nameTxt;
    private JTextField client_passwordTxt;
    private JTextField wipqTxt;

    /**
     * Create the frame.
     */
    public BuyWip() {
        setIconifiable(true);
        setClosable(true);
        setTitle("购买理财产品");
        setBounds(100, 100, 1004, 946);

        JLabel lblNewLabel = new JLabel("理财产品名称：");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));

        BI_nameText = new JTextField();
        BI_nameText.setColumns(10);

        JButton button = new JButton("查询 ");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                queryActionPerformed(e);
            }
        });
        button.setFont(new Font("宋体", Font.PLAIN, 20));

        JScrollPane scrollPane = new JScrollPane();


        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u8D2D\u4E70\u7406\u8D22\u4EA7\u54C1", TitledBorder.LEADING, TitledBorder.TOP, null, SystemColor.textHighlight));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(124)
                                                .addComponent(lblNewLabel)
                                                .addGap(49)
                                                .addComponent(BI_nameText, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE)
                                                .addGap(78)
                                                .addComponent(button, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(51)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(panel, GroupLayout.PREFERRED_SIZE, 895, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 890, GroupLayout.PREFERRED_SIZE))))
                                .addContainerGap(42, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(51)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                                        .addComponent(lblNewLabel)
                                                        .addComponent(BI_nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(48)
                                                .addComponent(button, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)))
                                .addGap(35)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 326, GroupLayout.PREFERRED_SIZE)
                                .addGap(43)
                                .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(51, Short.MAX_VALUE))
        );

        JLabel lblNewLabel_1 = new JLabel("理财产品编号：");

        JLabel lblNewLabel_2 = new JLabel("理财产品名称：");

        JLabel lblNewLabel_3 = new JLabel("理财产品描述：");

        JLabel lblNewLabel_4 = new JLabel("理财产品金额：");

        JLabel lblNewLabel_5 = new JLabel("理财产品年限：");

        wip_idTxt = new JTextField();
        wip_idTxt.setEditable(false);
        wip_idTxt.setColumns(10);

        wip_nameTxt = new JTextField();
        wip_nameTxt.setEditable(false);
        wip_nameTxt.setColumns(10);

        wip_desTxt = new JTextField();
        wip_desTxt.setEditable(false);
        wip_desTxt.setColumns(10);

        wip_amountTxt = new JTextField();
        wip_amountTxt.setEditable(false);
        wip_amountTxt.setColumns(10);

        wip_yearTxt = new JTextField();
        wip_yearTxt.setEditable(false);
        wip_yearTxt.setColumns(10);

        JLabel lblNewLabel_6 = new JLabel("用户姓名：");

        client_nameTxt = new JTextField();
        client_nameTxt.setColumns(10);

        JLabel lblNewLabel_7 = new JLabel("用户密码：");

        client_passwordTxt = new JTextField();
        client_passwordTxt.setColumns(10);

        JButton button_1 = new JButton("购买 ");
        button_1.setIcon(new ImageIcon(BuyWip.class.getResource("/image/Buy.png")));
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buyActionPerformed(e);
            }
        });

        JLabel lblNewLabel_8 = new JLabel("购买份数：");

        wipqTxt = new JTextField();
        wipqTxt.setColumns(10);
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(38)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
                                                                .addGroup(gl_panel.createSequentialGroup()
                                                                        .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                                                .addComponent(lblNewLabel_1)
                                                                                .addComponent(lblNewLabel_3))
                                                                        .addPreferredGap(ComponentPlacement.RELATED)
                                                                        .addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
                                                                                .addComponent(wip_desTxt, Alignment.TRAILING)
                                                                                .addComponent(wip_idTxt, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)))
                                                                .addGroup(gl_panel.createSequentialGroup()
                                                                        .addComponent(lblNewLabel_5)
                                                                        .addPreferredGap(ComponentPlacement.RELATED)
                                                                        .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                                                .addComponent(client_nameTxt, GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                                                                                .addComponent(wip_yearTxt))))
                                                        .addComponent(lblNewLabel_6))
                                                .addGap(100)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
                                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(lblNewLabel_7)
                                                                        .addComponent(lblNewLabel_4))
                                                                .addComponent(lblNewLabel_2))
                                                        .addComponent(lblNewLabel_8))
                                                .addGap(66)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
                                                        .addComponent(wip_amountTxt, 191, 191, Short.MAX_VALUE)
                                                        .addComponent(wip_nameTxt, GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                                                        .addComponent(client_passwordTxt)
                                                        .addComponent(wipqTxt)))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(334)
                                                .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(61, Short.MAX_VALUE))
        );
        gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGap(30)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(wip_idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(wip_nameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel_2))
                                .addGap(40)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_3)
                                        .addComponent(wip_desTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel_4)
                                        .addComponent(wip_amountTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(40)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_5)
                                        .addComponent(wip_yearTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel_8)
                                        .addComponent(wipqTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(33)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_6)
                                        .addComponent(client_nameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(client_passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel_7))
                                .addGap(37)
                                .addComponent(button_1)
                                .addContainerGap(46, Short.MAX_VALUE))
        );
        panel.setLayout(gl_panel);

        wip_table = new JTable();
        wip_table.setFillsViewportHeight(true);
        wip_table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                tableMousePressed(me);
            }
        });
        wip_table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "理财产品编号", "理财产品名称", "产品描述", "购买金额", "理财年限"
                }
        ) {
            boolean[] columnEditables = new boolean[]{
                    false, false, false, false, false
            };

            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });
        wip_table.getColumnModel().getColumn(1).setPreferredWidth(96);
        wip_table.getColumnModel().getColumn(2).setPreferredWidth(151);
        wip_table.getColumnModel().getColumn(3).setPreferredWidth(109);
        wip_table.getColumnModel().getColumn(4).setPreferredWidth(93);
        scrollPane.setViewportView(wip_table);
        getContentPane().setLayout(groupLayout);
        this.fillTable(new Wip());

    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BuyWip frame = new BuyWip();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void tableMousePressed(MouseEvent tme) {
        // TODO Auto-generated method stub
        int row = this.wip_table.getSelectedRow();
        this.wip_idTxt.setText(String.valueOf(wip_table.getValueAt(row, 0)));
        this.wip_nameTxt.setText(String.valueOf(wip_table.getValueAt(row, 1)));
        this.wip_desTxt.setText(String.valueOf(wip_table.getValueAt(row, 2)));
        this.wip_amountTxt.setText(String.valueOf(wip_table.getValueAt(row, 3)));
        this.wip_yearTxt.setText(String.valueOf(wip_table.getValueAt(row, 4)));
    }

    //
    private void buyActionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        String wip_id = this.wip_idTxt.getText();
        String wip_amount = this.wip_amountTxt.getText();
        if (StringUtils.isEmpty(wip_id)) {
            JOptionPane.showMessageDialog(null, "请选择要购买的理财产品！");
            return;
        }
        String client_name = this.client_nameTxt.getText();
        String client_password = this.client_passwordTxt.getText();
        String wipq = this.wipqTxt.getText();

        if (StringUtils.isEmpty(client_name)) {
            JOptionPane.showMessageDialog(null, "请输入用户姓名！");
            return;
        }

        if (StringUtils.isEmpty(client_password)) {
            JOptionPane.showMessageDialog(null, "请输入用户密码！");
            return;
        }
        if (StringUtils.isEmpty(wipq)) {
            JOptionPane.showMessageDialog(null, "请输入购买份数！");
            return;
        }

        ClientDAO clientDao = new ClientDAO();
        Client client = clientDao.queryClient(new Client(client_name)).get(0);
        Integer client_id = client.getClient_id();
        String password = client.getClient_password();


        if (password.equals(client_password)) {
            Wip wip = wipDAO.queryWip(new Wip(Integer.parseInt(wip_id))).get(0);
            Buy_Wip buy_wip = buy_wipDAO.buyWip(client, wip, Integer.parseInt(wipq));
            if (buy_wip != null) {
                JOptionPane.showMessageDialog(null, "购买成功！");
                resetValue();
            } else {
                JOptionPane.showMessageDialog(null, "购买失败！");
            }
        } else {
            JOptionPane.showMessageDialog(null, "密码错误！");
            this.client_passwordTxt.setText("");
            return;
        }

    }

    private void resetValue() {
        // TODO Auto-generated method stub
        this.wip_idTxt.setText("");
        this.wip_nameTxt.setText("");
        this.wip_desTxt.setText("");
        this.wip_amountTxt.setText("");
        this.wip_yearTxt.setText("");
        this.client_nameTxt.setText("");
        this.client_passwordTxt.setText("");
        this.wipqTxt.setText("");
    }


    /**
     * 查询事件处理
     *
     * @param evt
     */
    private void queryActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String wip_name = this.BI_nameText.getText();
        Wip wip = new Wip();
        wip.setWip_name(wip_name);
        this.fillTable(wip);
    }

    /**
     * 初始化表格
     *
     * @param wip
     */
    private void fillTable(Wip wip) {
        DefaultTableModel dtm = (DefaultTableModel) wip_table.getModel();
        dtm.setRowCount(0);//清空表格
        List<Wip> wipList = wipDAO.queryWip(wip);
        for (Wip rs : wipList) {
            Vector v = new Vector();
            String up = rs.getWip_up();
            if ("1".equals(up)) {
                v.add(rs.getWip_id());
                v.add(rs.getWip_name());
                v.add(rs.getWip_description());
                v.add(rs.getWip_amount());
                v.add(rs.getWip_year());
                dtm.addRow(v);
            }
        }

    }

}
